package com.hoyue.attendance.controller;

import com.hoyue.attendance.bean.AttendanceVo;
import com.hoyue.attendance.service.AttendanceService;
import com.hoyue.classroom.bean.Course;
import com.hoyue.classroom.service.CourseService;
import com.hoyue.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.*;

// MDAwMDU4
/**
 * @author zhouyong
 * @date 2019/8/20 21:03
 */

@Controller
@RequestMapping("/attendance")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private UserService userService;

    //定义全局变量AttendanceVo和List
    private static AttendanceVo attendanceVo;
    private static List<AttendanceVo> list = new ArrayList<>();

    // 定义学生id和课程号id和老师id的全局变量
    private Integer Studentid;
    private Integer teachingid;
    private Integer Courseid;
    private Integer NewStudentid;

    // 定义签到人数变量
    private static Integer num = 0;

    // 增加考勤
    @RequestMapping("/insertOne")
    public String insertOne(Model model, Integer id, Integer studentid, Integer teachedid, Integer state,
                            Integer typ, String starttime, Integer code, String endtime){
        attendanceVo = new AttendanceVo(userService.getLoginUser().getId(),null,null,null,null,null);
        // 增加考勤信息
        attendanceService.addAttence(id,studentid,attendanceVo.getTeachingid(),state,typ,starttime,code,endtime);
        // 将老师id赋值给全局变量
        teachingid = attendanceVo.getTeachingid();
        return "redirect:/attendance/selectAll";
    }

    // 更新学生考勤信息
    @ResponseBody
    @RequestMapping("/updateStudent")
    public String updateStudent(Integer state,Integer typ){
        // 教师端：更新学生信息
        AttendanceVo attendanceVo1 = new AttendanceVo(null,Studentid,state,typ,null,new Date());
        int count = attendanceService.updateStudent(attendanceVo1);
        if(count > 0) {
            return "redirect:/attendance/selectAll";
        }
        return null;
    }

    // 查询学生信息
    @RequestMapping("/selectAll")
    @RequiresRoles("teacher")
    public String selectAll(Model model, HttpSession session){
        // 获得本课堂的id
        Course course = (Course) session.getAttribute("course");
        // 赋值全局变量
        Courseid = course.getId();
        attendanceVo = new AttendanceVo(userService.getLoginUser().getId(),null,null,null,course.getName(),null);
        list = attendanceService.selectAll(attendanceVo);
        model.addAttribute("message",list);
        model.addAttribute("factNumber", list.size());
        model.addAttribute("course",course);
        model.addAttribute("attenceNum",num);
        return "attendance/attendance2";
    }

    // 学生进行签到
    @ResponseBody
    @RequestMapping("/attendIn")
    public int attendIn(Model model,Integer code){
        // typ的赋值可以为除了0,1,2之外的数字
        // 避免学生重复签到的冲突
        if(NewStudentid != userService.getLoginUser().getId()) {
            attendanceVo = new AttendanceVo(null, userService.getLoginUser().getId(), 1, 4, null,new Date());
            NewStudentid = userService.getLoginUser().getId();
            AttendanceVo attendanceVo1 = new AttendanceVo(code, teachingid);
            // 匹配学生输入的签到码
            Integer count = attendanceService.attendIn(attendanceVo1);
            // 获取当前课程名称
            model.addAttribute("course", courseService.getCourse(Courseid));
            if (count > 0) {
                // 当签到码正确时更新学生的签到信息
                Integer count1 = attendanceService.updateStudent(attendanceVo);
                if (count1 > 0) {
                    // 记录签到人数
                    num++;
                    return count;
                }else{
                    NewStudentid = null;
                }
            }
        }else{
            return 1;
        }
        return 0;
    }

    // 学生查询签到信息
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    @RequestMapping("/selectCourseAndTea")
    public String selectCourseAndTea(Model model,HttpSession session){
        // 获得本课堂的id
        Course course = (Course) session.getAttribute("course");
        // 赋值全局变量
        Courseid = course.getId();
        attendanceVo = new AttendanceVo(null,userService.getLoginUser().getId(),null,null,courseService.getCourse(Courseid).getName(),null);
        // 学生端：学生查询签到信息
        List<AttendanceVo> list1 = attendanceService.selectCourseAnduser(attendanceVo);
        model.addAttribute("message",list1);
        model.addAttribute("course",courseService.getCourse(Courseid));
        return "attendance/attendance2";
    }

    // 根据id查询学生信息
    @ResponseBody
    @RequestMapping("/selectStudentMess")
    public List<AttendanceVo> selectStudentMess(Model model,Integer studentid){
        Studentid = studentid;
        // 点击详情查询学生信息，姓名，学号，考勤类型，考勤方式
        List<AttendanceVo> list = attendanceService.selectStudentMess(studentid);
        model.addAttribute("studentMessage",list);
        return list;
    }

    // 根据老师姓名模糊查询签到信息
    @RequestMapping("/CheckMessage")
    public String CheckMessage(Model model,String NewUserName){
        attendanceVo = new AttendanceVo(userService.getLoginUser().getId(),courseService.getCourse(Courseid).getName(),NewUserName);
        // 学生端：学生查询签到信息
        model.addAttribute("message",attendanceService.checkMessage(attendanceVo));
        model.addAttribute("course",courseService.getCourse(Courseid));
        return "attendance/attendance2";
    }
}
